#Cairo canvas. The following variables are available:
# * CNTX: A reference to the current cairo context object
# * W: Canvas width
# * H: Canvas height
# Have fun!

class Figure:

    def __init__(self, c, h, w):
        self.color = c
        self.height = h
        self.width = w
        
    def set_color(self):
        CNTX.set_source_rgb( *COLOR(self.color) )

    def get_width(self):
        return self.width
        
    def get_height(self):
        return self.height        

    def get_color(self):
        return self.color        

class Ceiling(Figure):

    def __init__(self, c, h, w):
        Figure.__init__(self, c, h, w)
        
    def draw(self):
        CNTX.save()
        self.set_color()
        CNTX.move_to(0,self.height)
        CNTX.rel_line_to(self.width/2,-self.height)
        CNTX.rel_line_to(self.width/2,self.height)
        CNTX.rel_line_to(-self.width,0)
        CNTX.close_path()
        CNTX.fill()
        CNTX.restore()        
        
class Front(Figure):

    def __init__(self, c, h, w):        
        Figure.__init__(self, c, h, w)

    def draw(self):
        CNTX.save()
        self.set_color()
        CNTX.rectangle(0,0,self.width,self.height)
        CNTX.fill()
        CNTX.restore()

class House(Figure):

    def __init__(self, ceil, f, d):
        Figure.__init__(self, None, None, None)
        self.front = f
        self.ceiling = ceil
        self.door = d

    def draw(self):
        CNTX.save()        
        self.ceiling.draw()
        CNTX.translate(0, self.ceiling.get_height() )
        self.front.draw()
        CNTX.translate( (self.front.get_width() - self.door.get_width() ) / 2.0, self.front.get_height() - self.door.get_height() )
        self.door.draw()
        CNTX.restore()        

    def get_width(self):
        return self.front.get_width()
        
    def get_heigth(self):
        return self.front.get_heigth() + self.ceiling.get_heigth()

class Door(Figure):
    
    def __init__(self, c, h, w ):
        Figure.__init__( self, c, h, w )
        
    def draw(self):
        CNTX.save()        
        CNTX.set_line_width( self.width / 20.0 )
        self.set_color()
        CNTX.rectangle(0, 0, self.width, self.height )
        CNTX.stroke()
        CNTX.restore()                

class Window(Figure):
    
    def __init__(self, c, h, w ):
        Figure.__init__( self, c, h, w )
        
    def draw(self):
        CNTX.save()        
        CNTX.set_line_width( self.width / 20.0 )
        self.set_color()
        CNTX.rectangle(0, 0, self.width, self.height )
        CNTX.stroke()
        CNTX.move_to( 0, self.height / 2 )
        CNTX.rel_line_to( self.width, 0 )
        CNTX.stroke()        
        CNTX.move_to( self.width / 2, 0 )
        CNTX.rel_line_to( 0, self.height )
        CNTX.stroke()        
        CNTX.restore()                

class RichHouse(House):
    
    def __init__(self, ceil, f, d, left_win, rigth_win):
        House.__init__(self, None, None, None)
        self.front = f
        self.ceiling = ceil
        self.door = d
        self.left_win = left_win
        self.rigth_win = rigth_win

    def draw(self):
        House.draw(self)

        CNTX.save()
        CNTX.translate( self.front.get_width() / 8, self.ceiling.get_height() + (self.front.get_height() / 8 ) )
        self.left_win.draw()        
        CNTX.translate( self.front.get_width() / 8, self.ceiling.get_height() + (self.front.get_height() / 8 ) )
        CNTX.restore()        
        CNTX.save()
        CNTX.translate( 7*self.front.get_width()/8 - ( self.rigth_win.get_width() ) , self.ceiling.get_height() + (self.front.get_height() / 8 ) )
        self.rigth_win.draw()        
        CNTX.translate( self.front.get_width() / 8, self.ceiling.get_height() + (self.front.get_height() / 8 ) )
        CNTX.restore()        
        

CNTX.scale(20,20)

theCeiling = Ceiling("black", 2.0, 10.0)
theFront = Front("white", 5.0, 10.0)
theHouse = House( theCeiling, theFront, Door( theCeiling.get_color(), 2.0, 1.0 ) )
theHouse.draw()

theCeiling = Ceiling("darkgreen", 2.0, 10.0)
CNTX.translate( theHouse.get_width()+ 1, 0.0 )

h2 = RichHouse( theCeiling, Front("white", 5.0, 10.0), Door( theCeiling.get_color(), 2.0, 1.0 ), Window( "Black", 1.0, 2.0 ), Window( "Black", 1.0, 2.0 ) )
h2.draw()

theCeiling = Ceiling("navyblue", 2, 10)
CNTX.translate( h2.get_width()+ 1, 0.0 )
h3 = House( theCeiling, Front("white", 5, 10), Door( theCeiling.get_color(), 2.5, 1.5 ) )
h3.draw()
